﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using LaguzziCandidato.Clases;
using Newtonsoft.Json.Linq;
using BLLLaguzziCandidato;
using System.Web.Security;

namespace LaguzziCandidato
{
    public partial class Autenticar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string authToken = Request.QueryString["token"];

            if (authToken == null)
                return;

            string HitURL = string.Format("https://graph.facebook.com/me?access_token={0}", authToken);
            oAuthFacebook objFbCall = new oAuthFacebook();
            string JSONInfo = objFbCall.WebRequest(oAuthFacebook.Method.GET, HitURL, "");
            JObject Job = JObject.Parse(JSONInfo);
            JToken Jdata = Job.Root;

            if (Jdata.HasValues)
            {
                string email = (string)Jdata.SelectToken("email");

                if (new BLLUsuario().Validar(email))
                {
                    FormsAuthentication.SetAuthCookie(email, true);
                    Response.Redirect("~/Default.aspx", true);
                }
                else
                    lblResultado.Text = "[Accesinho no autorizadinho]";
            }
        }
    }
}